package com.itheima.service;

import com.itheima.client.addresspojo.dto.AddressAddDto;
import com.itheima.client.addresspojo.dto.AddressUpdateDto;
import com.itheima.client.addresspojo.vo.AddressAllVo;
import com.itheima.client.addresspojo.vo.AddressByIdVo;
import com.itheima.client.addresspojo.vo.AddressDefaultVo;
import com.itheima.client.addresspojo.vo.AddressVo;
import com.itheima.common.SnowFlake;
import com.itheima.common.TokenHolder;
import com.itheima.mapper.client.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wuzhiyong
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Repository
public class AddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private SnowFlake snowFlake;

    /**
     * 设置默认地址
     *
     * @param id id
     * @return addressDefaultVo
     */
    public AddressDefaultVo modifyIsDefault(String id) {

        //获取userId
        String userId = TokenHolder.getCurrentId();

        //设置默认地址
        addressMapper.updateIsDefault(id, userId);
        //重置其他地址为非默认
        addressMapper.updateNoDefault(id, userId);

        //组装响应体
        AddressDefaultVo addressDefaultVo = new AddressDefaultVo();
        addressDefaultVo.setIsDefault(1);
        addressDefaultVo.setId(id);
        return addressDefaultVo;
    }

    /**
     * 添加地址
     *
     * @param addressAddDto addressAddDto
     * @return List<AddressVo>
     */
    public List<AddressVo> addAddress(AddressAddDto addressAddDto) {
        //获取userId
        String userId = TokenHolder.getCurrentId();
        //通过雪花算法获取id
        long id = snowFlake.nextId();
        //组装响应体
        addressAddDto.setId(String.valueOf(id));
        addressAddDto.setUserId(userId);
        addressAddDto.setCreateUser(userId);
        addressAddDto.setUpdateUser(userId);
        //根据userId查询地址id，为0时为第一次添加
        List<String> list = addressMapper.selectIdByUserId(userId);
        if (list.size() == 0) {
            //第一次添加地址
            addressAddDto.setIsDefault(1);
        }else {
            //非第一次添加
            addressAddDto.setIsDefault(0);
        }
        addressMapper.insertAddress(addressAddDto);
        return addressMapper.selectById(String.valueOf(id));
    }

    /**
     * 删除地址
     * @param ids ids
     * @return 条数>0
     */
    public boolean removeAddressByIds(String[] ids) {
        if (ids == null) {
            return false;
        }
        for (String id : ids) {
            if (id == null) {
                continue;
            }

            //判断是否为默认地址，默认不能删
            int isDefault = addressMapper.selectIsDefault(id);
            if (isDefault == 1) {
                return false;
            }
        }
        return addressMapper.deleteAddressByIds(ids) > 0;
    }

    /**
     * 修改地址
     * @param addressUpdateDto addressUpdateDto
     * @return 条数>0
     */
    public boolean modifyAddressById(AddressUpdateDto addressUpdateDto) {
        return addressMapper.updateAddressById(addressUpdateDto) > 0;
    }

    /**
     * 获取默认收货地址
     * @return AddressDefaultVo
     */
    public AddressDefaultVo getDefault() {
        String userId = TokenHolder.getCurrentId();
        return addressMapper.selectDefault(userId);
    }

    /**
     * 获取收货地址列表
     * @return List<AddressAllVo>
     */
    public List<AddressAllVo> getAllAddress() {
        String userId = TokenHolder.getCurrentId();
        return addressMapper.selectAllAddress(userId);
    }

    /**
     * 根据ID获取收货地址接口
     * @param id id
     * @return AddressByIdVo
     */
    public AddressByIdVo getAddressById(String id) {
        return addressMapper.selectAddressById(id);
    }
}
